#ifndef ANIMATION_H
#define ANIMATION_H
#include <iosfwd>
#include <math.h>
#include "globals.h"
#include "camera.h"
namespace gemren
{
	struct animation_context
	{ 
		//data for simple runaround animation - camera revolves around <center> on trajectory start-end
		bool enabled;
		coord start, end;
		coord center;
		int framecnt;
		int frameno; //current rendered frame
		std::string filename;
		int spp; //per frame
		operator bool(){return enabled;}
		animation_context(): enabled(false){}
		void set_camera(gemren::camera& cam)
		{
			if(!enabled) return;
			if(frameno >= framecnt){
				enabled = false;
				return;
			}
			coord ps = to_polar(start), pe = to_polar(end);
			float t = float(frameno) / (framecnt - 1);
			coord ppos = t*pe + (1.0f-t)*ps;
			ppos = from_polar(ppos);
			cam.set_pos(ppos);
			cam.set_dir(center - ppos);
			++frameno;
		}
	protected:
		/* public Vector<double> Transform(Vector<double> v)
        { 
            return new Vector<double>(scaleX * v.X * Math.Cos(v.Y), scaleY * v.X * Math.Sin(v.Y), v.Z); 
        }
        public Vector<double> Inverse(Vector<double> v)
        {
            double x = v.X / scaleX;
            double y = v.Y / scaleY;
            double r = Math.Sqrt(x*x + y*y);
            double fi = r>1e-9? Math.Acos(x / r):0;
            if(y < 0) fi = (2*Math.PI)-fi;
            return new Vector<double>(r, fi, v.Z);
        }*/
		coord to_polar(const coord& pt_) const
		{
			coord pt = pt_ - center;
            float r = sqrt(pt.x*pt.x + pt.y*pt.y);
            float fi = r>1e-5? acos(pt.x / r):0.0f;
            if(pt.y < 0) fi = (2*3.141592654f)-fi;
            return coord(r, fi, pt.z);
		}
		coord from_polar(const coord& pt) const
		{
			return center + coord( pt.x * cos(pt.y), pt.x * sin(pt.y), pt.z); 
		}

	} ;
}
std::ostream& operator << (std::ostream& o, const gemren::animation_context& ac);

std::istream& operator >> (std::istream& in, gemren::animation_context& ac);
#endif